package com.cincc.cinccimdemo.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.cincc.cinccimdemo.R;
import com.cincc.cinccimdemo.common.ContactUtil;
import com.cincc.cinccimdemo.common.callback.OnItemClickListener;
import com.cincc.cinccimdemo.model.FriendInfoUI;
import com.cincc.im.model.dbmodel.FriendInfo;

import java.util.ArrayList;
import java.util.List;

public class ContactAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    Context mContext;
    private final List<FriendInfoUI> mList;

    public ContactAdapter(Context mContext, List<FriendInfoUI> mList) {
        this.mContext = mContext;
        this.mList = mList;
    }

    private static final int HEADER_ITEM = 0;
    private static final int DATA_ITEM = 1;

    @Override
    public int getItemViewType(int position) {
        if (position == 0)
            return HEADER_ITEM;
        else return DATA_ITEM;
    }

    public void setContactList(List<FriendInfo> conversationInfos) {
        this.mList.clear();
        //为了展示列表头 加了一条空数据
        this.mList.add(new FriendInfoUI());
        List<FriendInfoUI> temp = new ArrayList<>();
        for (FriendInfo friendInfo : conversationInfos) {
            FriendInfoUI item = ContactUtil.getFriendUI(friendInfo);
            temp.add(item);
        }
        if (temp.size() > 0) {
            for (String letter : ContactUtil.LETTER_LIST) {
                List<FriendInfoUI> beanList = ContactUtil.queryByContactHeader(temp, letter);
                this.mList.addAll(beanList);
            }
            notifyDataSetChanged();
        }
    }

    public List<FriendInfoUI> getList() {
        return mList;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == HEADER_ITEM) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.contact_header_view, parent, false);
            return new HeaderViewHolder(view);
        } else {
            View view = LayoutInflater.from(mContext).inflate(R.layout.list_contact_my_friend_item, parent, false);
            return new MyViewHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        FriendInfoUI model = mList.get(position);
        if (holder.getItemViewType() == HEADER_ITEM) {
            HeaderViewHolder headerViewHolder = (HeaderViewHolder) holder;
            headerViewHolder.newFriendL.setOnClickListener(view -> iOnHeaderClickItem.OnClick(0));
            headerViewHolder.groupL.setOnClickListener(view -> iOnHeaderClickItem.OnClick(1));
            headerViewHolder.organizationL.setOnClickListener(view -> iOnHeaderClickItem.OnClick(2));
        } else {
            MyViewHolder myViewHolder = (MyViewHolder) holder;

            myViewHolder.nameText.setText(model.nickName);

            myViewHolder.firstTv.setVisibility(TextUtils.isEmpty(model.protraitUrl) ? View.VISIBLE : View.GONE);
            myViewHolder.avatar.setVisibility(TextUtils.isEmpty(model.protraitUrl) ? View.GONE : View.VISIBLE);
            if (TextUtils.isEmpty(model.protraitUrl)) {
                myViewHolder.firstTv.setText(model.nickName.substring(0, 1));
            } else {
                Glide.with(mContext).load(model.protraitUrl).placeholder(R.mipmap.avatar_def)
                        .transforms(new CenterCrop(), new RoundedCorners(15))
                        .into(myViewHolder.avatar);
            }

            if (position == ContactUtil.getPositionForSection(position, 1, mList)) {
                myViewHolder.headerTv.setVisibility(View.VISIBLE);
                myViewHolder.headerTv.setText(model.getLetterName());
            } else {
                myViewHolder.headerTv.setVisibility(View.GONE);
            }

            myViewHolder.itemView.setOnClickListener(v -> {
                iOnDataClickItem.onClick(position);
            });
        }
    }

    public interface IOnHeaderClickItem {
        void OnClick(int index);
    }

    private IOnHeaderClickItem iOnHeaderClickItem;

    public void setOnHeaderClickItem(IOnHeaderClickItem iOnClickItem) {
        this.iOnHeaderClickItem = iOnClickItem;
    }

    private OnItemClickListener iOnDataClickItem;

    public void setOnDataClickItem(OnItemClickListener iOnClickItem) {
        this.iOnDataClickItem = iOnClickItem;
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    static class MyViewHolder extends RecyclerView.ViewHolder {
        TextView firstTv;
        TextView nameText;
        TextView headerTv;
        ImageView avatar;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            firstTv = itemView.findViewById(R.id.first_letter_tv);
            nameText = itemView.findViewById(R.id.mText_item_contact_name);
            headerTv = itemView.findViewById(R.id.mText_list_item_header);
            avatar = itemView.findViewById(R.id.img_avatar);
        }
    }

    static class HeaderViewHolder extends RecyclerView.ViewHolder {
        LinearLayout newFriendL;
        LinearLayout groupL;
        LinearLayout organizationL;

        public HeaderViewHolder(@NonNull View itemView) {
            super(itemView);
            newFriendL = itemView.findViewById(R.id.llNewFriend);
            groupL = itemView.findViewById(R.id.llGroup);
            organizationL = itemView.findViewById(R.id.llOrganization);
        }
    }
}
